package com.c78.fz.config;

import com.alibaba.fastjson.JSON;
import com.c78.fz.common.utils.WebUtils;
import com.c78.fz.common.result.AuthResult;
import com.c78.fz.common.result.ResultCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        AuthResult result = new AuthResult(ResultCode.UNAUTHORIZED.getCode(),"用户未认证，请重新登录",null);
        WebUtils.renderString(response, JSON.toJSONString(result));
    }
}
